import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';

class AppUpdaterProgressBar extends StatelessWidget {
  const AppUpdaterProgressBar({
    Key? key,
    this.gradient,
    this.trackColor,
    required this.progress,
  }) : super(key: key);

  final Gradient? gradient;
  final Color? trackColor;
  final double progress;

  @override
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);

    final Gradient gradient = this.gradient ??
        LinearGradient(
          begin: AlignmentDirectional.centerStart,
          end: AlignmentDirectional.centerEnd,
          colors: <Color>[
            theme.progressIndicatorTheme.color ?? theme.primaryColor,
            const Color(0xFF77C6FF),
          ],
        );
    final Color trackColor = this.trackColor ??
        theme.progressIndicatorTheme.linearTrackColor ??
        Theme.of(context).dividerColor;
    const double trackHeight = 10.0;

    final Decimal percentage =
        Decimal.fromJson(progress.toString()) * Decimal.fromInt(100);

    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.end,
      children: <Widget>[
        Text(
          '${percentage.toStringAsFixed(2)}%',
          style: TextStyle(
            color: theme.disabledColor,
            fontSize: 12,
            height: 16 / 12,
          ),
          textAlign: TextAlign.end,
        ),
        const SizedBox(height: 5),
        Stack(
          children: <Widget>[
            Container(
              width: double.infinity,
              height: trackHeight,
              decoration: BoxDecoration(
                color: trackColor,
                borderRadius: const BorderRadius.all(Radius.circular(5)),
              ),
            ),
            LayoutBuilder(
              builder: (BuildContext context, BoxConstraints constraints) {
                return Container(
                  width: constraints.hasBoundedWidth
                      ? constraints.maxWidth * progress
                      : 0,
                  height: trackHeight,
                  decoration: BoxDecoration(
                    borderRadius: const BorderRadius.all(Radius.circular(5)),
                    gradient: gradient,
                  ),
                );
              },
            ),
          ],
        ),
      ],
    );
  }
}
